package com.baizhi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baizhi.dto.Result;
import com.baizhi.dto.UserDTO;
import com.baizhi.entity.Follow;
import com.baizhi.entity.User;
import com.baizhi.mapper.FollowMapper;
import com.baizhi.service.IFollowService;
import com.baizhi.service.IUserService;
import com.baizhi.utils.RedisConstants;
import com.baizhi.utils.UserHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gange
 * @since 2023-04-06
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;
    @Override
    public Result isFollow(Long followUserId) {
        // 1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        // 2.查询是否关注 select count(*) from tb_follow where user_id = ? and follow_user_id = ?
        Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        // 3.判断
        return Result.ok(count > 0);
    }

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        String key =  RedisConstants.FOLLOW_KEY+userId;
        // 1.判断到底是关注还是取关
        if (isFollow) {
            // 2.关注，新增数据
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean isSuccess = save(follow);
            //把用户放入到好友列表中 redis
            redisTemplate.opsForSet().add(key,followUserId);

        } else {
            // 3.取关，删除 delete from tb_follow where user_id = ? and follow_user_id = ?
            remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId).eq("follow_user_id", followUserId));
            //把用户移除好友列表中 redis
            redisTemplate.opsForSet().remove(key,followUserId);

        }
        return Result.ok();
    }

    @Override
    public Result comminFollow(Long id) {
        //1 获取登录用户
        Long userId = UserHolder.getUser().getId();
        //2 使用redis的set的求交集的命令查看两个集合里面的共同好友
        String mykey = RedisConstants.FOLLOW_KEY+userId;
        String commonkey = RedisConstants.FOLLOW_KEY+id;
        Set<Long> idSet = redisTemplate.opsForSet().intersect(mykey, commonkey);
        if (ObjectUtil.isEmpty(idSet)){
            return Result.ok(Collections.emptyList());
        }
        //3 根据查出来的用户id生成userDTO放入到redis集合中fan'hui
        List<UserDTO> userDTOS = new ArrayList<>(idSet.size());
        List<User> users = userService.listByIds(idSet);
        //判断集合是否为空
        boolean empty = CollectionUtils.isEmpty(users);
        if (!empty){
            //不为空转换
            for (User user:users){
                userDTOS.add(BeanUtil.copyProperties(user,UserDTO.class));
            }
        }
        return  Result.ok(userDTOS);
    }
}
